package com.molichuxing.framework.utils;


public class HtmlUtil {
	public static void main(String[] args) {
		String ss = "<a href='#'>aaa</a><script>alert(2);</script>";
		System.out.println(toHTML(ss, false));
	}

	public static String toHTML(String org, boolean inputValue) {
		StringBuffer result = new StringBuffer(org.length());
		char[] chars = org.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (chars[i] == '\"') {
				result.append("&quot;");
			} else if (chars[i] == '<') {
				result.append("&lt;");
			} else if (chars[i] == '>') {
				result.append("&gt;");
			} else if (chars[i] == '&') {
				result.append("&amp;");
			} else if (chars[i] == '\r') {
				if (inputValue) {
					result.append(chars[i]);
				} else {
					result.append("<br/>");
					if (i + 1 < chars.length && chars[i + 1] == '\n') {
						i++;
					}
				}
			} else if (chars[i] == '\n') {
				if (inputValue) {
					result.append(chars[i]);
				} else {
					result.append("<br/>");
				}
			} else if (chars[i] == ' ') {
				if (inputValue) {
					result.append(chars[i]);
				} else {
					result.append("&nbsp;");
				}
			} else {
				result.append(chars[i]);
			}
		}
		return result.toString();
	}
}
